package com.sk.bilibili.tool;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sk.bilibili.bean.Them;
import com.sk.bilibili.bean.Video;
import com.sk.bilibili.bean.VideoSet;
import com.sk.bilibili.service.MinioService;
import com.sk.bilibili.service.ThemService;
import com.sk.bilibili.service.VideoService;
import com.sk.bilibili.service.VideoSetService;
import com.sk.bilibili.utils.QueryWrapperUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 邵康
 * @date 2021/12/8 12:03
 */
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class TidyVideo {

    // 番剧数量
    @Autowired
    private VideoService videoService;
    @Autowired
    private VideoSetService videoSetService;
    @Autowired
    private MinioService minioService;
    @Autowired
    private ThemService themService;

    @Test
    void test8(){//番剧翻一番
        List<Them> list = themService.list(null);
        themService.saveBatch(list);
    }

    @Test
    void test9(){
        QueryWrapper<VideoSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("type_id",2,3);
        List<VideoSet> list = videoSetService.list(queryWrapper);
        List<Them> thems = themService.list(null);
        int index=0;
        for (Them them:thems){
            them.setVideoSetId(list.get(index%list.size()).getId());
            index++;
            themService.updateById(them);
        }
    }

    @Test//翻倍 数量
    void test6(){
        List<VideoSet> list = videoSetService.list(null);
        videoSetService.saveBatch(list);
    }

    @Test
    void test5(){
        List<String> list = minioService.list("video/url");
        System.out.println(list.size());
    }

    @Test// 为视频集随机命名
    void test4(){
        List<Video> list = videoService.list(null);
        List<String> collect = list.stream().map(Video::getTitle).distinct().collect(Collectors.toList());
        int index=0;
        List<VideoSet> list1 = videoSetService.list(null);
        Random random = new Random();
        for (VideoSet videoSet:list1){
            if(index<collect.size()){
                videoSet.setTitle(collect.get(index));
            }else {
                videoSet.setTitle(collect.get(random.nextInt(collect.size())));
            }
            videoSetService.updateById(videoSet);
            index++;
        }
    }

    @Test//设置 VideoSet的视频信息
    void test2(){
        List<Video> list = videoService.list(null);
        int index=1;
        Random random = new Random();
        for (Video video:list){
            if(index>668){
                video.setVideoSetId(random.nextInt(668)+1);
            }else {
                video.setVideoSetId(index);
            }
            videoService.updateById(video);
            index++;
        }
    }

    @Test//添加视频信息
    void test7(){
        List<Video> list = videoService.list(null);
        for (int i = 0; i < 800; i++) {
            videoService.save(list.get(i));
        }
    }

    @Test//设置所有 VideoSet的时长信息
    void test3(){
        List<VideoSet> list = videoSetService.list(null);
        for (VideoSet videoSet:list){
            QueryWrapper<Video> queryWrapper = QueryWrapperUtils.eq("video_set_id", videoSet.getId());
            List<Video> videos = videoService.list(queryWrapper);
            Optional<Integer> reduce = videos.stream().map(Video::getDuration).reduce(Integer::sum);
            videoSet.setDuration(reduce.get());
            videoSetService.updateById(videoSet);
        }
    }

    @Test//混淆所有视频
    void test1(){
        Map<String, Video> map=new HashMap<>();
        List<Video> list = videoService.list(null);
        for (Video video:list){
            map.put(video.getUrl(),video);
        }
        int index=0;
        List<Video> target=new ArrayList<>(map.values());
        for (Video video:list){
            Video temp = target.get(index % target.size());
            video.setVideoSetId((index%334)+1);
            video.setTitle(temp.getTitle());
            video.setUrl(temp.getUrl());
            video.setDuration(temp.getDuration());
            videoService.updateById(video);
            index++;
        }
    }


}
